using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace MemoryGame
{
    public class StateSelectLevel : DEF
    {
        public static int NUM_ROW = 4;
        public static int NUM_COL = 4;
        public static int MAX_PAG = 1;
        public static int mcurrentPage = 0;
        public static int SELECTLEVEL_BUTTON_W = 0;
        public static int SELECTLEVEL_BUTTON_H = 0;
        public static int SELECTLEVEL_BEGIN_Y = 0;
        public static int SELECTLEVEL_BEGIN_X = 0;
        public static int BUTTON_ARROW_CONFIRM_W = 60;
        public static int BUTTON_ARROW_CONFIRM_H = 60;

        public static TBitmaps BITMAP_THUMBS;
        public static TButtonCenter[][] BUTTON_THUMBS;

        public static Texture2D selectLevelBg;
        public static void SendMessage(int type)
        {

            switch (type)
            {
                case MESSAGE_CTOR:
                    selectLevelBg = Game1.contentManager.Load<Texture2D>("image//screen_1280x800");
                   // mcurrentPage = Game1.mLevelUnlock / (NUM_ROW * NUM_COL);

                    SELECTLEVEL_BUTTON_H = (int)(DEF.texttureButtonMenu[DEF.FRAME_SELECTLEVEL_NORMAL].Height * Game1.SCALE_Y);
                    SELECTLEVEL_BUTTON_W = (int)(DEF.texttureButtonMenu[DEF.FRAME_SELECTLEVEL_NORMAL].Width * Game1.SCALE_X);
                    SELECTLEVEL_BEGIN_Y = (SCREEN_HEIGHT - ((SELECTLEVEL_BUTTON_H + DEF.SELECTLEVEL_CONTENT_SPACE_H) * (NUM_ROW - 1))) / 2 - 20;
                    DEF.SELECTLEVEL_CONTENT_SPACE_H = DEF.SELECTLEVEL_CONTENT_SPACE_H / 4;
                    SELECTLEVEL_BEGIN_X = (SCREEN_WIDTH - ((SELECTLEVEL_BUTTON_W + DEF.SELECTLEVEL_CONTENT_SPACE_W) * (NUM_COL - 1))) / 2;
                    DEF.SELECTLEVEL_CONTENT_SPACE_H = ((SCREEN_HEIGHT - SELECTLEVEL_BEGIN_Y) - NUM_ROW * SELECTLEVEL_BUTTON_H - SCREEN_HEIGHT / 10) / (NUM_ROW - 1);

                    BUTTON_ARROW_CONFIRM_W = (int)(DEF.texttureButtonMenu[DEF.FRAME_BUTTON_LEFT_NORMAL].Height * Game1.SCALE_Y);
                    BUTTON_ARROW_CONFIRM_H = (int)(DEF.texttureButtonMenu[DEF.FRAME_BUTTON_LEFT_NORMAL].Width * Game1.SCALE_X);
                 //   SoundManager.pausesoundLoop(0);
                    BITMAP_THUMBS = new TBitmaps("image//thum//thumb", 0, 4, DEF.SCREEN_WIDTH / 6, -1);
                    BUTTON_THUMBS = new TButtonCenter[4][];

                    int mINX = 10;
                    int mwidth = DEF.SCREEN_WIDTH - 2 * mINX;

                    int DX = (DEF.SCREEN_WIDTH - 2 * mINX) / 4;
                    int DY = (int)(DEF.SCREEN_HEIGHT*.92 - SELECTLEVEL_BEGIN_Y)/4;
                   // Game1.instanceGame1.loadLevelUnlock();
                    for (int i = 0; i < 4; i++)
                    {
                        BUTTON_THUMBS[i] = new TButtonCenter[4];
                        for (int j = 0; j < 4; j++)
                        {
                            if (T.I1 == Map.gameDifficultMode)
                            {
                                if (i * 4 + j <= T.I2)
                                    BUTTON_THUMBS[i][j] = new TButtonCenter(BITMAP_THUMBS.bitmaps[T.GAME_DATA[Map.gameDifficultMode][i * 4 + j]], mINX + j * DX + BITMAP_THUMBS.width_half, SELECTLEVEL_BEGIN_Y + i * DY + BITMAP_THUMBS.height_half, BITMAP_THUMBS.scale_x);
                                else
                                    BUTTON_THUMBS[i][j] = new TButtonCenter(BITMAP_THUMBS.bitmaps[4], mINX + j * DX + BITMAP_THUMBS.width_half, SELECTLEVEL_BEGIN_Y + i * DY + BITMAP_THUMBS.height_half, BITMAP_THUMBS.scale_x);
                            }
                            else
                            {
                                BUTTON_THUMBS[i][j] = new TButtonCenter(BITMAP_THUMBS.bitmaps[T.GAME_DATA[Map.gameDifficultMode][i * 4 + j]], mINX + j * DX + BITMAP_THUMBS.width_half, SELECTLEVEL_BEGIN_Y + i * DY + BITMAP_THUMBS.height_half, BITMAP_THUMBS.scale_x);
                            }
                        }
                    }
                    break;
                case MESSAGE_UPDATE:
                    int index = 0;
                    for (int i = 0; i < NUM_ROW; i++)
                    {
                        for (int j = 0; j < NUM_COL; j++)
                        {
                            if (BUTTON_THUMBS[i][j].Kick_First())
                            {

                                if (T.I1 == Map.gameDifficultMode && i * 4 + j > T.I2)
                                {
                                    Guide.BeginShowMessageBox("Warning", "Cannot play the locked level", new List<string> { "OK" }, 0, MessageBoxIcon.Warning,
                                        asyncResult =>
                                        {
                                            int? returned = Guide.EndShowMessageBox(asyncResult);
                                        }, null);
                                }
                                else
                                {
                                    T.CURRENT_I1 = Map.gameDifficultMode;
                                    T.CURRENT_I2 = 4 * i + j;
                                    Game1.changeState(Game1.STATE_GAMEPLAY);
                                }
                                
                            }
                        }
                    }

                    //left right button
                    if (MAX_PAG > 1)
                        if (InputState.isTouchReleaseInRect(SELECTLEVEL_BEGIN_X - 3 * BUTTON_ARROW_CONFIRM_W / 2, SCREEN_HEIGHT / 2, DEF.DIALOG_BUTTON_CONFIRM_W, DEF.DIALOG_BUTTON_CONFIRM_H))
                        {
                            mcurrentPage--;
                            if (mcurrentPage < 0)
                                mcurrentPage = MAX_PAG - 1;
                            SoundManager.playSound(SoundManager.SOUND_SELECT);
                        }

                    //right	
                    if (MAX_PAG > 1)
                        if (InputState.isTouchReleaseInRect(SCREEN_WIDTH - (SELECTLEVEL_BEGIN_X - BUTTON_ARROW_CONFIRM_W / 2), SCREEN_HEIGHT / 2, DEF.DIALOG_BUTTON_CONFIRM_W, DEF.DIALOG_BUTTON_CONFIRM_H))
                        {
                            mcurrentPage++;
                            if (mcurrentPage >= MAX_PAG)
                                mcurrentPage = 0;
                            SoundManager.playSound(SoundManager.SOUND_SELECT);
                        }

                    if (InputState.isBackKeyPress() || InputState.isTouchReleaseInRect(DEF.SCREEN_WIDTH - 5 * DEF.BUTTON_CANCEL_CONFIRM_W / 4, DEF.SELECTLEVEL_BACKGROUND_Y + 8, DEF.BUTTON_CANCEL_CONFIRM_W, DEF.BUTTON_CANCEL_CONFIRM_H))
                    {
                        SoundManager.playSound(SoundManager.SOUND_BACK);
                        Game1.changeState(Game1.STATE_SELECT_MODE);
                        break;
                    }
                    //if (GameLib.isTouchReleaseScreen()) {
                    //	changeState(STATE_GAMEPLAY);
                    //}

                    break;
                case MESSAGE_PAINT:

                    Game1.spriteBatch.Draw(selectLevelBg, new Vector2(0, 0), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);
                    string text = "";
                    Vector2 tempMeasure;
                    if (MAX_PAG > 1)
                    {
                        text = "Page : " + (mcurrentPage + 1) + "/" + MAX_PAG;
                        tempMeasure = Game1.fontNormal.MeasureString(text);
                        Game1.spriteBatch.DrawString(Game1.fontNormal, text, new Vector2(DEF.SCREEN_WIDTH / 2 - tempMeasure.X / 2, DEF.SELECTLEVEL_BACKGROUND_Y - 2), Color.White);
                    }
                    else
                    {
                        text = "SELECT LEVEL";
                        tempMeasure = Game1.fontNormal.MeasureString(text);
                        Game1.spriteBatch.DrawString(Game1.fontNormal, text, new Vector2(DEF.SCREEN_WIDTH / 2 - tempMeasure.X / 2, DEF.SELECTLEVEL_BACKGROUND_Y - 2), Color.Blue);
                    }


                    for (int i = 0; i < NUM_ROW; i++)
                    {
                        int y = SELECTLEVEL_BEGIN_Y + i * (SELECTLEVEL_BUTTON_H + DEF.SELECTLEVEL_CONTENT_SPACE_H);
                        int x = 0;
                        for (int j = 0; j < NUM_COL; j++)
                        {
                            x = SELECTLEVEL_BEGIN_X + j * (SELECTLEVEL_BUTTON_W + DEF.SELECTLEVEL_CONTENT_SPACE_W);
                            BUTTON_THUMBS[i][j].Draw(ref Game1.spriteBatch);
                            //Game1.mcurrentlevel = mcurrentPage * NUM_COL * NUM_ROW + i * NUM_ROW + j;
                            //if (Game1.mcurrentlevel <= Game1.mLevelUnlock)
                            //{
                            //    if (InputState.isTouchDrapInRect(x - SELECTLEVEL_BUTTON_W / 2, y - SELECTLEVEL_BUTTON_H / 2, SELECTLEVEL_BUTTON_W, SELECTLEVEL_BUTTON_H))
                            //        .Draw(texttureButtonMenu[DEF.FRAME_SELECTLEVEL_HIGHTLIGHT], new Vector2(x - SELECTLEVEL_BUTTON_W / 2, y - SELECTLEVEL_BUTTON_H / 2), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);
                            //    else
                            //        Game1.spriteBatch.Draw(texttureButtonMenu[DEF.FRAME_SELECTLEVEL_NORMAL], new Vector2(x - SELECTLEVEL_BUTTON_W / 2, y - SELECTLEVEL_BUTTON_H / 2), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);
                            //    text = " " + (mcurrentPage * NUM_COL * NUM_ROW + i * NUM_ROW + j + 1) + " ";
                            //    tempMeasure = Game1.fontNormal.MeasureString(text);
                            //    Game1.spriteBatch.DrawString(Game1.fontNormal, text, new Vector2(x - tempMeasure.X / 2, y - SELECTLEVEL_BUTTON_H / 2), Color.White);


                            //    if (Game1.mcurrentlevel != Game1.mLevelUnlock)
                            //    {
                            //        //	fontbig_Yellow.drawString(mainCanvas, "Pass", x, y - fontbig_Yellow.getFontHeight()/2  , BitmapFont.ALIGN_CENTER);
                            //    }
                            //    else
                            //    {
                            //        text = "New";
                            //        tempMeasure = Game1.fontNormal.MeasureString(text);
                            //        Game1.spriteBatch.DrawString(Game1.fontNormal, text, new Vector2(x - tempMeasure.X / 2, y + SELECTLEVEL_BUTTON_H / 2 - tempMeasure.Y), Color.White);
                            //    }

                            //}
                            //else
                            //{
                            //    Game1.spriteBatch.Draw(texttureButtonMenu[DEF.FRAME_SELECTLEVEL_LOCK], new Vector2(x - SELECTLEVEL_BUTTON_W / 2, y - SELECTLEVEL_BUTTON_H / 2), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);
                            //}

                        }
                    }
                    //select left and right button
                    if (MAX_PAG > 1)
                        if (InputState.isTouchDrapInRect(BUTTON_ARROW_CONFIRM_W / 2, SCREEN_HEIGHT / 2, BUTTON_ARROW_CONFIRM_W, BUTTON_ARROW_CONFIRM_H))
                            Game1.spriteBatch.Draw(texttureButtonMenu[DEF.FRAME_BUTTON_LEFT_HIGHTLIGHT], new Vector2(BUTTON_ARROW_CONFIRM_W / 2, SCREEN_HEIGHT / 2), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);
                        else
                            Game1.spriteBatch.Draw(texttureButtonMenu[DEF.FRAME_BUTTON_LEFT_NORMAL], new Vector2(BUTTON_ARROW_CONFIRM_W / 2, SCREEN_HEIGHT / 2), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);
                    //right	
                    if (MAX_PAG > 1)
                        if (InputState.isTouchDrapInRect((SCREEN_WIDTH - 3 * BUTTON_ARROW_CONFIRM_W / 2), SCREEN_HEIGHT / 2, BUTTON_ARROW_CONFIRM_W, BUTTON_ARROW_CONFIRM_H))
                            Game1.spriteBatch.Draw(texttureButtonMenu[DEF.FRAME_BUTTON_RIGHT_HIGHTLIGHT], new Vector2(SCREEN_WIDTH - 3 * BUTTON_ARROW_CONFIRM_W / 2, SCREEN_HEIGHT / 2), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);
                        else
                            Game1.spriteBatch.Draw(texttureButtonMenu[DEF.FRAME_BUTTON_RIGHT_NORMAL], new Vector2(SCREEN_WIDTH - 3 * BUTTON_ARROW_CONFIRM_W / 2, SCREEN_HEIGHT / 2), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);


                    //draw cancel
                    if (InputState.isTouchDrapInRect(DEF.SCREEN_WIDTH - 5 * DEF.BUTTON_CANCEL_CONFIRM_W / 4, DEF.SELECTLEVEL_BACKGROUND_Y + 8, DEF.BUTTON_CANCEL_CONFIRM_W, DEF.BUTTON_CANCEL_CONFIRM_H))
                        Game1.spriteBatch.Draw(texttureButtonMenu[DEF.FRAME_CANCEL_HIGHTLIGHT], new Vector2(DEF.SCREEN_WIDTH - 5 * DEF.BUTTON_CANCEL_CONFIRM_W / 4, DEF.SELECTLEVEL_BACKGROUND_Y + 8), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);
                    else
                        Game1.spriteBatch.Draw(texttureButtonMenu[DEF.FRAME_CANCEL_NORMAL], new Vector2(DEF.SCREEN_WIDTH - 5 * DEF.BUTTON_CANCEL_CONFIRM_W / 4, DEF.SELECTLEVEL_BACKGROUND_Y + 8), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);


                    break;
                case MESSAGE_DTOR:
                    break;
            }
        }
    }
}
